package com.ht.medicaltreatment.activity

import android.text.TextUtils
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.gym.baselib.base.BaseActivity
import com.gym.baselib.utils.CacheUtils
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.base.CacheKey
import com.ht.medicaltreatment.http.api.SaveSampleBoxUpdateAPI
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.view.RefrigeratorLocationDialog
import com.ht.medicaltreatment.view.SampleFormalDialog
import com.ht.medicaltreatment.view.ShowModelDialog
import com.ht.medicaltreatment.view.TaskDeleteDialog
import com.ht.medicaltreatment.viewmodel.SampleBoxViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_update_sample_box.btnDelete
import kotlinx.android.synthetic.main.activity_update_sample_box.btnUpdate
import kotlinx.android.synthetic.main.activity_update_sample_box.etName
import kotlinx.android.synthetic.main.activity_update_sample_box.llFormat
import kotlinx.android.synthetic.main.activity_update_sample_box.llLocation
import kotlinx.android.synthetic.main.activity_update_sample_box.llModel
import kotlinx.android.synthetic.main.activity_update_sample_box.llUID
import kotlinx.android.synthetic.main.activity_update_sample_box.tvCreateName
import kotlinx.android.synthetic.main.activity_update_sample_box.tvGuiGe
import kotlinx.android.synthetic.main.activity_update_sample_box.tvLocation
import kotlinx.android.synthetic.main.activity_update_sample_box.tvRFID
import kotlinx.android.synthetic.main.activity_update_sample_box.tvShowType
import kotlinx.android.synthetic.main.activity_update_sample_box.tvTime

/**
 * 修改样本盒
 */
class SampleBoxUpdateActivity:BaseActivity(), View.OnClickListener {

    override fun getLayoutId() = R.layout.activity_update_sample_box

    private lateinit var mModel:SampleBoxViewModel
    protected var id = -1
    //行
    private var sampleBoxStructureRow = ""
    //列
    private var sampleBoxStructureCol = ""
    //样本盒位置id
    private var sampleBoxStruId  = ""
    //UID
    private var rfidCode   = ""
    //样本盒名称
    private var sampleBoxName   = ""
    //显示模式
    private var sampleBoxStructureSpec   = ""

    override fun init() {
        mModel = ViewModelProvider(this)[SampleBoxViewModel::class.java]
        val name = CacheUtils.getStringCache(CacheKey.NAME)
        tvCreateName.text = name

        setTitle("修改样本盒信息")
        id = intent.getIntExtra("id",-1)

        btnDelete.setOnClickListener(this)
        llFormat.setOnClickListener(this)
        llModel.setOnClickListener(this)
        llLocation.setOnClickListener(this)
        llUID.setOnClickListener(this)
        btnUpdate.setOnClickListener(this)

        mModel.detail(id){

            sampleBoxStructureRow = "${it.sampleBoxStru.structureRow}"
            sampleBoxStructureCol = "${it.sampleBoxStru.structureCol}"
            sampleBoxStruId = "${it.sampleBoxStru.id}"
            rfidCode = it.rfidCode
            sampleBoxName = it.name
            sampleBoxStructureSpec = "${it.sampleBoxStru.structureSpec}"

            etName.setText(it.name)
            tvGuiGe.text = "${it.sampleBoxStru.structureRow} * ${it.sampleBoxStru.structureCol}"
            tvShowType.text = ""
            tvLocation.text = it.sampleBoxStru.detailLocation
            tvRFID.text = it.rfidCode
            tvTime.text = it.inputTime
            when(it.sampleBoxStru.structureSpec){
                1 -> tvShowType.text = "数字 * 数字"
                2 -> tvShowType.text = "数字 * 字母"
                3 -> tvShowType.text = "字母 * 数字"
                4 -> tvShowType.text = "字母 * 字母"
                5 -> tvShowType.text = "内部数字"
            }
        }

    }

    override fun onClick(v: View?) {
        when(v){
            //删除
            btnDelete -> {
                TaskDeleteDialog(this).showDialog {
                    showProgress()
                    mModel.delete(id,{
                        dismissProgress()
                        showToast("删除成功")
                        finish()
                    })
                }
            }

            //规格
            llFormat -> SampleFormalDialog(this).show { H, L ->
                sampleBoxStructureRow = H
                sampleBoxStructureCol = L
                tvGuiGe.text = "$H*$L"
            }

            //显示模式
            llModel -> ShowModelDialog(this).show { name, id ->
                sampleBoxStructureSpec = "$id"
                tvShowType.text = name
            }

            //存放位置
            llLocation -> RefrigeratorLocationDialog(this).show { name, id ->
                tvLocation.text = name
                sampleBoxStruId = id
            }

            //绑定RFID
            llUID -> ScanUtils.open {
                rfidCode = it
                tvRFID.text = it
            }

            //修改
            btnUpdate -> {
                sampleBoxName = etName.text.toString()
                if(TextUtils.isEmpty(sampleBoxName))return
                val bean = SaveSampleBoxUpdateAPI(
                    id = id,
                    name = sampleBoxName,
                    rfidCode = rfidCode,
                    sampleBoxStruId = sampleBoxStruId,
                    sampleBoxStructureCol = sampleBoxStructureCol,
                    sampleBoxStructureRow = sampleBoxStructureRow,
                    sampleBoxStructureSpec = sampleBoxStructureSpec
                )

                showProgress()
                mModel.update(bean){
                    dismissProgress()
                    showToast("修改成功")
                    finish()
                }
            }
        }
    }
}